<template>
  <div>
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @confirm="onConfirm"
      @cancel="$emit('clicks')"
    />
  </div>
</template>

<script>
import { patchuSerProfile } from '@/api/user.js'
import dayjs from 'dayjs'
export default {
  data () {
    return {
      currentDate: new Date(this.value),
      minDate: new Date(1900, 1, 1),
      maxDate: new Date()
    }
  },
  props: {
    value: {
      type: String,
      require: true
    }
  },
  created () {

  },

  methods: {
    async onConfirm (val) {
      try {
        const currDate = dayjs(val).format('YYYY-MM-DD')
        await patchuSerProfile({
          birthday: currDate
        })
        this.$toast.success('更新成功')
        this.$emit('clicks')
        this.$emit('input', currDate)
      } catch (err) {
        this.$toast.fail('更新失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
